package com.yinhe.music.yhmusic.db.dao;

import com.hwangjr.rxbus.RxBus;
import com.j256.ormlite.dao.Dao;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusListObject;
import com.yinhe.music.yhmusic.application.MusicApplication;
import com.yinhe.music.yhmusic.db.AbstractBaseDb;
import com.yinhe.music.yhmusic.db.DbHelper;
import com.yinhe.music.yhmusic.db.bean.DownloadMusicEntity;
import com.yinhe.music.yhmusic.db.bean.RecentMusicDBEntity;
import com.yinhe.music.yhmusic.downmusic.DownloadStatus;
import com.yinhe.music.yhmusic.log.MLog;
import com.yinhe.music.yhmusic.utils.MusicUtils;

import java.util.List;


/**
 * Created by wangyang on 17/3/18.
 */

public class DownloadMusicEntityDAO extends AbstractBaseDb {

    private static final String TAG = DownloadMusicEntityDAO.class.getSimpleName();
    private Dao dao;


    public DownloadMusicEntityDAO(DbHelper dbHelper) {
        super(dbHelper);
        dao = getDao(DownloadMusicEntity.class);
    }

    public void insertDownMusicSyn(final DownloadMusicEntity downloadDBEntity) {
        try {
            MLog.debug(TAG, "insertDownMusicSyn=" + downloadDBEntity);
            dao.createIfNotExists(downloadDBEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean updateDownMusic(final DownloadMusicEntity downloadDBEntity) {
        try {
            dao.update(downloadDBEntity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateDownMusicList(final List<DownloadMusicEntity> downloadMusicEntityList) {
        try {
            for (int i = 0; i < downloadMusicEntityList.size(); i++) {
                dao.update(downloadMusicEntityList.get(i));
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteDownMusicByList(final List<Integer> idList, boolean deleteFile) {
        try {
            for (Integer i : idList) {
                //删除本地
                if (deleteFile){
                    DownloadMusicEntity downloadMusicEntity = getDownMusicByDownloadIdSyn(i);
                    if (downloadMusicEntity != null){
                        MusicUtils.deleteFile(MusicApplication.getContext(),downloadMusicEntity);
                        MusicUtils.refreshLocal(MusicApplication.getContext(),downloadMusicEntity.getFileName(),downloadMusicEntity.uri);
                    }
                }
                dao.deleteById(i);
            }
            RxBus.get().post(RxBusEventType.Music.DELETE_DOWN_MUSIC, RxbusListObject.instance(idList));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }




    public boolean isExsitIdSyn(final long downID, int status) {
        try {
            DownloadMusicEntity info = (DownloadMusicEntity) dao.queryForId(downID);
            if (info != null && info.getDownloadStatus() != 5) {
                MLog.debug(TAG, info.getSongname() + "——downStatus:" + info.getDownloadStatus());
            }
            if (info != null && status == info.getDownloadStatus()) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public DownloadMusicEntity getDownMusicByDownloadIdSyn(final long downID) {
        try {
            return (DownloadMusicEntity) dao.queryForId(downID);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<DownloadMusicEntity> getDownMusicByDownloadStatus(final int downloadStatus) {
        try {
            List<DownloadMusicEntity> info = dao.queryForEq(DownloadMusicEntity.RecentSongColums.DOWNLOADSTATUS, downloadStatus);
            return info;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public DownloadMusicEntity getDownMusicByPath(String filePath) {
        try {
            List<DownloadMusicEntity> list = dao.queryForEq(DownloadMusicEntity.RecentSongColums.URI, filePath);
            if (list!=null && list.size()>0){
                return list.get(0);
            } else {
                MLog.debug("DownMusicDao","return null");
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            MLog.debug("DownMusicDao","ERROR"+e.toString());
            return null;
        }
    }

    public long getDownMusicCount() {
        try {
            return dao.queryBuilder().countOf();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public List<DownloadMusicEntity> getDowningMusic() {
        try {
            List<DownloadMusicEntity> info = dao.queryBuilder().where().
                    ne(RecentMusicDBEntity.RecentSongColums.DOWNLOADSTATUS, DownloadStatus.DOWNLOAD_STATUS_COMPLETED).
                    and().ne(RecentMusicDBEntity.RecentSongColums.DOWNLOADSTATUS, DownloadStatus.DOWNLOAD_STATUS_CANCEL).query();
            return info;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
